package net.nagaseyasuhito.nicola.component;

import net.nagaseyasuhito.nicola.entity.User;
import net.nagaseyasuhito.nicola.page.UserPage;
import net.nagaseyasuhito.nicola.service.UserService;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;

import com.google.inject.Inject;

public class FriendsPanel extends Panel {
	private static final long serialVersionUID = 1L;

	@Inject
	private UserService userService;

	public FriendsPanel(String id, User user) {
		super(id);

		this.add(new ListView("friends", new CompoundPropertyModel(this.userService.findFriendsByUser(user, 0, 10))) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem item) {
				item.setModel(new CompoundPropertyModel(item.getModelObject()));

				User friend = (User) item.getModelObject();
				PageParameters pageParameters = new PageParameters();
				pageParameters.add("name", friend.getName());

				item.add(new BookmarkablePageLink("user", UserPage.class, pageParameters).add(new Image("image", new NicolaImageResource(friend.getImage(), 32))));
			}
		});
	}
}
